#include "logindialog.h"
#include <QMouseEvent>
#include <qcryptographichash.h>
#include <qsettings.h>
#include <qmessagebox.h>

LoginDialog::LoginDialog(QWidget *parent)
	: QDialog(parent)
	, ui(new Ui::LoginDialogClass())
{
	ui->setupUi(this);
	setAttribute(Qt::WA_DeleteOnClose);
	//setWindowFlags(Qt::FramelessWindowHint);//无边框
	setWindowFlags(Qt::SplashScreen);

	QApplication::setOrganizationName("myself cai");
	QApplication::setApplicationName("07_05_Splash");
	readSettings();
}

LoginDialog::~LoginDialog()
{
	delete ui;
}

QString LoginDialog::encrypt(const QString& str)
{
	QCryptographicHash hash(QCryptographicHash::Md5);
	hash.addData(str.toLocal8Bit());
	return hash.result();
}

void LoginDialog::readSettings()
{
	QSettings settings;
	bool saved = settings.value("saved", false).toBool();
	userName = settings.value("Username", "userName").toString();
	password = encrypt("12345");
	/*password = settings.value("password", encrypt("12345")).toString();*/
	if (saved)
	{
		ui->lineEdit_UserName->setText(userName);
		ui->checkBox->setChecked(true);
	}
}

void LoginDialog::writeSettings()
{
	//保存设置，将设置写入注册表
	QSettings settings;
	settings.setValue("Username", userName);
	settings.setValue("password", password);
	settings.setValue("saved", ui->checkBox->isChecked());
}

void LoginDialog::mouseMoveEvent(QMouseEvent* event)
{
	qDebug() << "mouseMoveEvent";
	QPoint eventGloublePoint = event->globalPosition().toPoint();//当前鼠标事件全局位置

	//int length = (eventGloublePoint - leftPressGloublePos).manhattanLength();	
	//if (moving //处于移动状态
	//	&& (event->buttons() & Qt::LeftButton) //左键有按下，注意是buttons有s
	//	&&  length > QApplication::startDragDistance())//移动超过一定距离
	//{	
	//	//move是窗口左上角相对于全局，比如move(100,200)则本窗口左上角移动到(100,200)
	//	//拖动过程中鼠标相对窗口位置应不变
	//	this->move(eventGloublePoint - leftPressPos);

	//	//更新数据
	//	leftPressGloublePos = event->globalPos();
	//	leftPressPos = event->position().toPoint();		
	//}
	//return QDialog::mouseMoveEvent(event);

	//本人实验证明，不进行移动超过一定距离判断，不更新数据效果好，不清楚为什么
	if (moving //处于移动状态
		&& (event->buttons() & Qt::LeftButton)) //左键有按下，注意是buttons有s
	{
		//move是窗口左上角相对于全局，比如move(100,200)则本窗口左上角移动到(100,200)
		//拖动过程中鼠标相对窗口位置应不变
		this->move(eventGloublePoint - leftPressPos);
	}
}

void LoginDialog::mousePressEvent(QMouseEvent* event)
{
	qDebug() << "mousePressEvent";

	//窗口的pos是本窗口左上角全局位置
	qDebug() << "this->pos()" << this->pos();

	//事件的pos是相对于本窗口左上角
	qDebug() << "event->pos()" << event->pos();
	//事件的globalPos是相对于屏幕左上角
	qDebug() << "event->globalPos()" << event->globalPosition().toPoint();
	

	if (event->button() == Qt::LeftButton)//左键点击
	{
		moving = true;
		
		leftPressPos = event->pos();//记录鼠标左击事件窗口位置
		leftPressGloublePos = event->globalPosition().toPoint();//记录鼠标左击事件全局位置
	}
	return QDialog::mousePressEvent(event);//交给父类
}

void LoginDialog::mouseReleaseEvent(QMouseEvent* event)
{
	qDebug() << "mouseReleaseEvent";
	moving = false;
	event->accept();
}

void LoginDialog::on_pushButton_OK_clicked()
{
	QString userName_input = ui->lineEdit_UserName->text().trimmed();
	QString password_input = ui->lineEdit_Passeword->text().trimmed();
	QString encrptPassword = encrypt(password_input);
	if (userName_input == userName && encrptPassword == password)
	{
		writeSettings();
		this->accept();
	}
	else
	{
		tryCount++;
		if (tryCount > maxTryCount)
		{
			QMessageBox::critical(this, "错误", "输入错误次数太多");
			this->reject();
		}
		else
		{
			QMessageBox::warning(this, "错误", "用户名密码错误");
		}
	}
	
}
